package com.bsoft.gol.hcs.clinical.dao;


import com.bsoft.gol.hcs.clinical.entity.Account;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.transform.Transformers;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class AccountDAO extends HibernateSupportDelegateDAO<Account> {
    @DAOMethod(sql = " from Account where id =:id ")
    public abstract Account getAppDoctor(@DAOParam("id") Long id);

    @DAOMethod(sql = " from Account where realname =:REALNAME and mobile = :MOBILE and cardtype = :CARDTYPE and IDCARD = :IDCARD and FLAG = '1' ")
    public abstract Account getAccountByInfo(@DAOParam("REALNAME") String name, @DAOParam("MOBILE") String mobile,
                                       @DAOParam("CARDTYPE") String cardType, @DAOParam("IDCARD") String idCard);

    @DAOMethod(sql = " from Account where patientcode =:patientcode ")
    public abstract List<Account> queryAccountListByPatientcode(@DAOParam("patientcode") String patientcode);


    public Map<String,Object> getAccountFamilyBirthday(final String patientCode) {
        HibernateStatelessResultAction<Map<String,Object>> action = new AbstractHibernateStatelessResultAction<Map<String,Object>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("select BIRTHDATE as birthday from bas_account_family where PATIENTCODE=:patientCode LIMIT 1 ");
                Query query = ss.createSQLQuery(sql.toString());
                query.setParameter("patientCode", patientCode);
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map<String,Object>> list = query.list();
                if (list.size() == 1) {
                    setResult(list.get(0));
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public List<String> queryUserIdByLocalId(final String localOrgId,final String localDoctorId) {
        HibernateStatelessResultAction<List<String>> action = new AbstractHibernateStatelessResultAction<List<String>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append(" SELECT a.id userId from  base_user a,base_orgdoctors b,doc_identity c,base_orgmain d ");
                sql.append(" where a.id=c.idOrNo and b.orgId=d.orgId and b.doctorId=c.doctorId ");
                sql.append(" and c.idType='1' and d.localOrgId='"+localOrgId+"' and b.localDoctorId='"+localDoctorId+"' limit 1 ");
                Query query = ss.createSQLQuery(sql.toString());
                query = (SQLQuery) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = query.list();

                List<String> deptIdList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(dbList)) {
                    for (Map map : dbList) {
                        deptIdList.add((String) map.get("userId"));
                    }
                    setResult(deptIdList);
                } else {
                    setResult(null);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        List<String> userIdList = action.getResult();
        return userIdList;
    }

    public String queryOrgNameByLocalOrgId(final String localOrgId) {
        HibernateStatelessResultAction<String> action = new AbstractHibernateStatelessResultAction<String>() {
            @Override
            public void execute(StatelessSession s) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append(" SELECT title as orgName from app_organization where code=:code  ");
                Query query = s.createSQLQuery(sql.toString());
                query.setParameter("code", localOrgId);
                query = (SQLQuery) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                List<Map> list = query.list();
                if (CommonUtils.isNotEmpty(list)) {
                    for (Map map : list) {
                        setResult(map.get("orgName").toString());
                    }
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}
